package com.ufo.store.management.widget.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.ufo.store.management.utils.Utils;


public class CircleProgress extends View {
    private int viewWidth =0;
    private int viewHeight =0;
    private Paint paintArc;
    private Paint paintDot;
    private float default_stroke_width;
    private final int default_stroke_color = Color.rgb(199, 00, 00);
    private RectF rectF = new RectF();
    private float mRadius = 0;
    private float mRotateAngle = 0;
    private boolean isContinue = false;
    private ValueAnimator animator ;
    private boolean mbShowSmallArc = true;

    public CircleProgress(Context context) {
        this(context, null);
    }

    public CircleProgress(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        default_stroke_width = Utils.dp2px(getResources(), 4);
        initPainters();
    }

    private void initPainters(){
        paintArc = new Paint();
        paintArc.setAntiAlias(true);
        paintArc.setDither(true);
        paintArc.setStyle(Paint.Style.STROKE);
        paintArc.setStrokeCap(Paint.Cap.ROUND);
        paintArc.setStrokeWidth(default_stroke_width);
        paintArc.setColor(default_stroke_color);

        paintDot = new Paint();
        paintDot.setAntiAlias(true);
        paintDot.setDither(true);
        paintDot.setStyle(Paint.Style.FILL);
        paintDot.setStrokeCap(Paint.Cap.ROUND);
        paintDot.setStrokeWidth(default_stroke_width);
        paintDot.setColor(default_stroke_color);
    }

    public void startProgress() {
        isContinue = true;
        animator = ValueAnimator.ofFloat(0, 360);
        animator.setDuration(2000);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                Float integer = (Float) valueAnimator.getAnimatedValue();
                Draw(integer);
            }
        });
    }

    public void stopProgress() {
        isContinue = false;
        animator.cancel();
        invalidate();
    }

    public void setArcVisible(boolean is){
        mbShowSmallArc = is;
    }

    public boolean isProcessing(){
        return isContinue;
    }

    private void Draw(float rotate){
        mRotateAngle = rotate;
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        viewWidth = MeasureSpec.getSize(widthMeasureSpec);
        viewHeight = MeasureSpec.getSize(heightMeasureSpec);
        rectF.set(2f * Utils.dp2px(getResources(), 4), 2f * Utils.dp2px(getResources(), 4), width - 2 * Utils.dp2px(getResources(), 4), width - 2 * Utils.dp2px(getResources(), 4));
        mRadius = width / 2 - 2 * Utils.dp2px(getResources(), 4)  ;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float startAngle =0;
        float finishedAngle = 360;

        //画大圆
        canvas.drawArc(rectF, startAngle, finishedAngle, false, paintArc);

        //画旋转的小圆
        if(mbShowSmallArc) {
            double x = viewWidth/2 + Math.cos(2 * Math.PI / 360 * mRotateAngle) * mRadius;
            double y = viewHeight/2 + Math.sin(2 * Math.PI / 360 * mRotateAngle) * mRadius ;
            canvas.drawCircle((float) x, (float) y, Utils.dp2px(getResources(), 8), paintDot);
        }
    }
}
